﻿using System.Linq;
using System.Data.Linq;
using UniMana.Data;

namespace UniMana.Data
{
    public static class Personnel_Personnel_SubjectData
    {
        /// <summary>
        /// Lấy danh sách môn học giảng viên chưa đăng ký
        /// </summary>
        /// <param name="nPersonSubject"></param>
        /// <param name="m_strPersonnel_PersonnelKey"></param>
        /// <returns></returns>

        public static IQueryable GetSubjectNotRegist(this Table<Personnel_Personnel_Subject> nPersonnel_Personnel_Subject,
            Table<Personnel_Personnel_SubjectRegist> nPersonnel_Personnel_SubjectRegist,
            Table<Training_Subject> nTraining_Subject,
            string m_strPersonnel_PersonnelKey)
        {
            var values = from p in nPersonnel_Personnel_Subject
                         join s in nTraining_Subject on p.Training_SubjectKey equals s.Training_SubjectKey
                         where p.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                         where
                         !
                         (
                            from b in nPersonnel_Personnel_SubjectRegist
                            where p.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                            select b.Training_SubjectKey
                         ).Contains(p.Training_SubjectKey)
                         select new
                         {
                             s.Catalogue_DepartmentKey,
                             p.Training_SubjectKey,
                             s.ID,
                             s.Name,
                             s.TotalCredits
                         };
            return values.Distinct();
        }

        /// <summary>
        /// Lấy danh sách môn học giảng viên chưa đăng ký có trong học kỳ tiếp theo
        /// </summary>
        /// <param name="nPersonSubject"></param>
        /// <param name="nSubjectOpen"></param>
        /// <param name="m_strPersonnel_PersonnelKey"></param>
        /// <returns></returns>

        public static IQueryable GetSubjectNotRegistInTerm(this Table<Personnel_Personnel_Subject> nPersonnel_Personnel_Subject,
            Table<Personnel_Personnel_SubjectRegist> nPersonnel_Personnel_SubjectRegist,
            Table<Training_Subject_Open> nTraining_Subject_Open,
            Table<Training_Subject> nTraining_Subject,
            string m_strPersonnel_PersonnelKey)
        {
            var values = from p in nPersonnel_Personnel_Subject
                         join s in nTraining_Subject on p.Training_SubjectKey equals s.Training_SubjectKey
                         where p.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                         where
                         !(from b in nPersonnel_Personnel_SubjectRegist
                           where b.Personnel_PersonnelKey.ToString() == m_strPersonnel_PersonnelKey
                           select b.Training_SubjectKey
                         ).Contains(p.Training_SubjectKey)
                         where
                         (from c in nTraining_Subject_Open
                          where c.Closed == false
                          select c.Training_SubjectKey
                         ).Contains(p.Training_SubjectKey)
                         select new
                         {
                             s.Catalogue_DepartmentKey,
                             p.Training_SubjectKey,
                             s.ID,
                             s.Name,
                             s.TotalCredits
                         };
            return values.Distinct();
        }
    }
}
